<template>
  <div class="cart">
    <myheader titleName='购物车'></myheader>
    <div class="empty-cart" v-if='cartListPage.length === 0'>
      <img src="" alt="" class="empty-cart-img">
      <div class="empty-cart-text">购物车还是空的</div>
      <div class="btn">去逛逛</div>
    </div>
    <div class="cart-container" v-else v-for='(item,index) in cartListPage' :key='item.id'>
      <!-- 根据数据中每一项的选中状态，动态的添加类名 -->
      <!-- 1.3 给图标绑定事件 -->
      <span class="iconfont toogle" :class="item.selectState?'icon-checkbox-marked-circ':'icon-checkbox-blank-circle-outline'" @click='toggleState(index)'></span>
      <div class="item-detail">
        <img :src="item.img" :alt="item.title">
        <div class="item-info">
          <h3>{{item.title}}</h3>
          <div class="pay">
            <div class="pay-price">￥{{item.price}}</div>
            <div class="edit-quantity">
              <p class="operate-btn iconfont icon-minus" @click='handleMinus(index)'></p>
              <p class="btn-input">{{item.num}}</p>
              <p class="operate-btn iconfont icon-plus" @click='handlePlus(index)'></p>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="cart-footer">
      <div class="cart-footer-left"><span class="iconfont toogle" :class="isSelectAll?'icon-checkbox-marked-circ':'icon-checkbox-blank-circle-outline'" @click='toggleAll'></span><span>全选</span></div>
      <div class="cart-footer-center"><span>合计：</span><span class="total-price">￥{{totalPrice}}</span></div>
      <div class="cart-footer-right"><span class="goto-pay">结算 {{totalNum}}</span></div>
    </div>
  </div>
</template>
<script>
import myheader from '@/components/myheader'
import {MessageBox} from 'mint-ui'
export default {
  components: {
    myheader
  },
  data () {
    return {
      cartListPage: [] // 获取本地的数组
    }
  },
  created () {
    this.cartListPage = JSON.parse(localStorage.getItem('cartList') || '[]')
  },
  computed: {
    // 1.1 创建总数量计算属性
    totalNum () {
      let total = 0
      this.cartListPage.map(item => {
        // 循环页面数组，将所有项的num数量加起来
        total += item.num
      })
      return total
    },
    // 1.2 总价格计算属性
    totalPrice () {
      let total = 0
      this.cartListPage.map(item => {
        if (item.selectState) {
          total = item.num * item.price
        }
      })
      return total
    },
    // 1.4.1 创建一个控制全选非全选的计算属性
    isSelectAll () {
      let selectAll = true
      this.cartListPage.map(item => {
        // 1.4.2 对数组进行遍历，里面只要有一个数据项的状态为false，那么这个计算属性就为false
        if (item.selectState === false) {
          selectAll = false
        }
      })
      return selectAll
    }
  },
  // 1.8 将用户操作之后的页面数据更新到本地
  beforeDestroy () {
    console.log('before destroy')
    localStorage.setItem('cartList', JSON.stringify(this.cartListPage))
  },
  methods: {
    // 1.3 切换选中状态
    toggleState (index) {
      this.cartListPage[index].selectState = !this.cartListPage[index]
        .selectState
    },
    // 1.5 切换所有
    toggleAll () {
      // 只要isSelectAll 计算属性为true，就将数组中的每一项的状态变为false
      // 只要isSelectAll 计算属性为false，就将数组中的每一项的状态变为true
      let allStatus = this.isSelectAll
      // if (allStatus) {
      //   this.cartListPage.map(item => {
      //     item.selectState = false
      //   })
      // } else {
      //   this.cartListPage.map(item => {
      //     item.selectState = true
      //   })
      // }
      // 上面的if else 判断可以简写成下面这样
      this.cartListPage.map(item => {
        item.selectState = !allStatus
      })
    },
    // 1.6 减数量
    handleMinus (idx) {
      if (this.cartListPage[idx].num === 1) {
        MessageBox({
          title: '提示',
          message: '确定删除该商品？',
          showCancelButton: true
        })
        // 1.7 不管是点击取消还是确定，都是.then函数中处理的数据
          .then(action => {
          // 如果点击确定，jiu删除那一项
            if (action === 'confirm') {
              this.cartListPage.splice(idx, 1)
            }
          })
      } else {
        this.cartListPage[idx].num -= 1
      }
    },
    // 加数量
    handlePlus (idx) {
      this.cartListPage[idx].num += 1
    }
  }
}
</script>
<style lang="scss" scoped>
  .cart {
    height: 320px;
    margin-top: 45px;
    font-size: 14px;
    .empty-cart {
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: flex-end;
      height: 100%;
      .empty-cart-img {
        height: 90px;
        width: 90px;
      }
      .empty-cart-text {
        font-size: 20px;
        color: #999;
        padding: 15px 0;
      }
      .btn {
        font-size: 20px;
        padding: 15px 55px;
        text-align: center;
        margin: 0 auto;
        border-radius: 10px;
        background: #ed601b;
        color: #fff;
      }
    }
    .cart-container {
      display: flex;
      align-items: center;
      margin: 5px;
      padding: 8px 10px;
      border: 1px solid #eeeeee;
      border-radius: 20px;
      .toogle {
        width: 20px;
        height: 20px;
        line-height: 20px;
        font-size: 20px;
      }
      .icon-checkbox-marked-circ {
        color: #ff5500;
      }
      .icon-checkbox-blank-circle-outline {
        color: #666666;
      }
    }
    .item-detail {
      display: flex;
      flex: 1;
      img {
        margin: 2px 0 2px 5px;
        width: 90px;
        height: 90px;
      }
      .item-info {
        width: 100%;
        padding: 5px 10px;
        display: flex;
        flex-direction: column;
        justify-content: space-around;
        h3 {
          font-size: 16px;
          font-weight: 400;
        }
        .pay {
          display: flex;
          justify-content: space-between;
          .pay-price {
            color: #ff5500;
            font-weight: bold;
            line-height: 30px;
          }
          .edit-quantity {
            display: flex;
            height: 30px;
            line-height: 30px;
            .operate-btn {
              padding: 0 10px;
              font-size: 20px;
              color: #bababa;
            }
            .btn-minus {
              font-size: 14px;
            }
          }
        }
      }
    }
    .cart-footer {
      display: flex;
      justify-content: space-between;
      position: fixed;
      bottom: 0;
      left: 0;
      height: 50px;
      line-height: 50px;
      width: 100%;
      border-top: 1px solid #e7e7e7;
      background-color: #fff;
      .cart-footer-left {
        display: flex;
        justify-content: cneter;
        align-items: center;
        span {
          display: block;
          height: 50px;
          padding: 0 5px;
        }
      }
      .cart-footer-center {
        .total-price {
          color: #ff5500;
          font-weight: bold;
        }
      }
      .cart-footer-right {
        display: flex;
        flex-direction: column;
        padding: 0 20px;
        background-color: #ff5500;
        .goto-pay {
          color: #fff;
        }
      }
      .icon-checkbox-marked-circ {
        color: #ff5500;
      }
      .icon-checkbox-blank-circle-outline {
        color: #666666;
      }
    }
  }
</style>
